package com.wm.web.action;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.User;
import com.wm.comm.Constant;
import com.wm.comm.MD5;
import com.wm.service.UserService;
import com.wm.web.servlet.UserCookie;
import com.wm.web.servlet.UserSession;

public class LoginAction extends BaseAction implements SessionAware{

	private static final Log logger = LogFactory.getLog(LoginAction.class);

	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	private String username;
	private String password;
	private String returnURL;
	private String urlType;
	private String error;

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public String getUrlType() {
		return urlType;
	}

	public void setUrlType(String urlType) {
		this.urlType = urlType;
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getReturnURL() {
		return returnURL;
	}

	public void setReturnURL(String returnURL) {
		this.returnURL = returnURL;
	}
	
	private Map session;
	
    public void setSession(Map session){
    	this.session = session;
    }
    
    public Map getSession() {
		return session;
	}
    
	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception ex) {
			logger.error(ex);
			return ERROR;
		}
	}
	
	public String index() {
		if(Constant.USER_COOKIES){
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpServletResponse response = ServletActionContext.getResponse();
			UserCookie userCookie = new UserCookie(request,response);
			String uid = userCookie.getCookie(Constant.USER_COOKIE_KEY);
			if(uid!=null && !uid.trim().equals("")){
				User u = this.getUserService().findUserById(new Long(uid));
				if(u!=null){
					UserSession us = new UserSession();
					us.setUid(u.getId());
					us.setEmail(u.getUserEmail());
					us.setUsername(u.getUserName());
					this.session.put(Constant.USER_SESSION_KEY, us);
				}
			}
		}
		return "index";
	}
	
	public String login(){
		if(this.getSession().get(Constant.USER_SESSION_KEY)!=null){
			this.addFieldError("error", this.getText("error.login.already"));
			return "index";
		}
		if(this.getUsername()!=null && this.getPassword()!=null){
			MD5 md5 = new MD5();
			User u = this.getUserService().findUserByUserNameUserPwd(this.getUsername(), md5.getMD5ofStr(this.getPassword()));
			if(u!=null){
				UserSession us = new UserSession();
				us.setUid(u.getId());
				us.setEmail(u.getUserEmail());
				us.setUsername(u.getUserName());
				this.session.put(Constant.USER_SESSION_KEY, us);
				
				//将用户id写入cookies
				if(Constant.USER_COOKIES){
					HttpServletRequest request = ServletActionContext.getRequest();
					HttpServletResponse response = ServletActionContext.getResponse();
					UserCookie userCookie = new UserCookie(request,response);
					userCookie.addCookie(Constant.USER_COOKIE_KEY, String.valueOf(us.getUid()));
				}
				
				this.setUsername(u.getUserName());
			}else{
				this.addFieldError("error", this.getText("error.login"));
				this.setError("1");
				return "index";
			}
		}
		if(this.getReturnURL()!=null){
			this.setReturnURL(this.getReturnURL().replaceAll(",", "&"));
			return "returnUrl";
		}
		return "index";
	}
	
	public String logout(){
		if(this.getSession().get(Constant.USER_SESSION_KEY)!=null){
			this.session.remove(Constant.USER_SESSION_KEY);
			if(Constant.USER_COOKIES){
				HttpServletRequest request = ServletActionContext.getRequest();
				HttpServletResponse response = ServletActionContext.getResponse();
				UserCookie userCookie = new UserCookie(request,response);
				userCookie.removeCookie(Constant.USER_SESSION_KEY);
			}
		}
		if(this.getReturnURL()!=null){
			this.setReturnURL(this.getReturnURL().replaceAll(",", "&"));
			return "returnUrl";
		}
		return "returnUrl";
	}
}
